package cn.juque.common.thread;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.IdUtil;

/**
 * @author juque
 * @version 1.0.0
 * <ul>
 *     <li>TraceLogTreadLocal</li>
 * </ul>
 * @date 2024-03-13
 **/
public class TraceLogTreadLocal {

    private static final ThreadLocal<String> CURRENT_LOG_ID = ThreadUtil.createThreadLocal(false);

    public void set(String logId) {
        CURRENT_LOG_ID.set(logId);
    }

    public String get() {
        String logId = CURRENT_LOG_ID.get();
        if (CharSequenceUtil.isEmpty(logId)) {
            logId = IdUtil.fastSimpleUUID();
            CURRENT_LOG_ID.set(logId);
        }
        return logId;
    }

    public void remove() {
        CURRENT_LOG_ID.remove();
    }
}
